#!/bin/bash
echo "beginning wrf dry run at `date`"

start_date=$1
echo "Model simulation time: $start_date"

echo "System time: `date`"
WRF_dir="/public/users/wrf/Models/WRFV3.4"
HOME_DIR="/public/users/wrf"
WPS_ROOT=${WRF_dir}/WPS
WRF_ROOT=${WRF_dir}/WRFV3
WRFOUT_ROOT=${HOME_DIR}/wrfout
WRFLOG_ROOT=${HOME_DIR}/wrflog

WRFOUT_dir=$WRFOUT_ROOT/$start_date
WRFLOG_dir=$WRFLOG_ROOT/$start_date

if [ -n "$1" ]
then
    echo "mkdir $WRFOUT_dir"
    mkdir "$WRFOUT_dir"
    echo "mkdir $WRFLOG_dir"
    mkdir "$WRFLOG_dir"
else
    echo "Usage: $0 YYYYMMDDHH"
    exit 1
fi

echo "============= Beginning WPS Process ============="
cd ${WPS_ROOT}

#
# 1. running geogrid.exe to generate geo_em.d0?.nc files
#
echo "begin geogrid.exe at `date`"

rm -f geogrid.log*
rm -f ungrib.log*
rm -f metgrid.log*
rm -f mygeogrid.o*
rm -f mymetgrid.o*
rm -f myungrib.o*
rm -f geo_em.d*.nc
rm -f met_em.d*.nc
rm -f GRIBFILE*
rm -f PFILE*
rm -f FILE*

echo "Copy namelist.wps from ${WRF_ROOT}/test/em_real/"
cp -f ${WRF_ROOT}/test/em_real/namelist.wps ${WPS_ROOT}/

GEOG_JOB_NAME=`qsub geogrid.torque`
while qstat | grep ${GEOG_JOB_NAME}
do
    echo "wait for geogrid process $GEOG_JOB_NAME"
    sleep 10s
done

#cat geogrid.e"${GEOG_JOB_NAME}"
#cat geogrid.o"${GEOG_JOB_NAME}"

#
# 2. running ungrib.exe to generate FILE:200?-* files
#
echo "begin ungrib.exe at `date`"


${WPS_ROOT}/link_grib.csh /public/users/wrf/gfs2/gfs*

UNGRIB_JOB_NAME=`qsub ungrib.torque`
while qstat | grep ${UNGRIB_JOB_NAME}
do 
    echo "wait for ungrib process $UNGRIB_JOB_NAME"
    sleep 10s
done

#
# 3. running metgrid.exe to generate met_em.d0?.* files
#
echo "begin metgrid.exe at `date`"

#delete garbage
rm -f met_em.d0*

MET_JOB_NAME=`qsub metgrid.torque`
while qstat | grep ${MET_JOB_NAME}
do 
    echo "wait for metgrid process $METGRID_JOB_NAME"
    sleep 10s
done

#
# 4. save all log files to wrflog
#
mv metgrid.log* ungrib.log* geogrid.log* mygeogrid.o* myungrib.o* mymetgrid.o* $WRFLOG_dir

echo "System time: `date`"
echo "============= WPS Process Finished ============="

########### WPS Running finished #################################

########################## run wrf ###############################
echo "============= Beginning WRF Process ============="

cd ${WRF_ROOT}/test/em_real

rm -f met_em.d0*.nc
rm -f wrf_real_input*
rm -f wrfinput_*
rm -f wrfbdy_*
rm -f rsl.*
rm -f wrfout_d*
rm -f myreal.o*
rm -f mywrf.o*

ln -sf ${WPS_ROOT}/met_em.d0* .

#
# running real.exe
#
echo "begin real.exe at `date`"
REAL_JOB_NAME=`qsub real.torque`
while qstat | grep ${REAL_JOB_NAME}
do 
    echo "wait for real.exe process $REAL_JOB_NAME"
    sleep 10s
done

mkdir $WRFLOG_dir/real_log
mv rsl.* $WRFLOG_dir/real_log

#
# running wrf.exe
#
echo "begin wrf.exe at `date`"
WRF_JOB_NAME=`qsub wrf.torque`
while qstat | grep ${WRF_JOB_NAME}
do
    echo "wait for wrf.exe process $WRF_JOB_NAME"
    sleep 20s
done

mkdir $WRFLOG_dir/wrf_log
mv rsl.* $WRFLOG_dir/wrf_log

#
# mv WPS and WRF output to $WRFOUT_dir
#
echo "Copying model results to $WRFOUT_dir"
mkdir $WRFOUT_dir/metgrid
mv ${WPS_ROOT}/met_em.d*.nc $WRFOUT_dir/metgrid/

mkdir $WRFOUT_dir/geogrid
mv ${WPS_ROOT}/geo_em.d*.nc $WRFOUT_dir/geogrid/

mkdir $WRFOUT_dir/ungrib
mv ${WPS_ROOT}/FILE\:* $WRFOUT_dir/ungrib/

# make hard links to wrfout files
ln wrfout_d0* $WRFOUT_dir

echo "Model end at System time: `date`"
echo "============= WRF Process Finished ============="


exit 0
